<?php
if(!defined('IN_DISCUZ')) { //安全判断
	exit('Access Denied');
}

/* 
* @Author: ldm
* @Date:   2014-05-29 20:11:21
* @Last Modified by:   dm
* @Last Modified time: 2014-06-03 15:21:26
*/
$p_vars = $_G['cache']['plugin']['ii_video'];
// 发现插件未开启
if(!$p_vars['v_on']){
	showmessage('拼命维护中……');
}
// error_reporting(E_ALL);
class video{
	public $catid; 				// 显示文章总栏目id
	public $now_catid; 			// 当前栏目id	
	public $now_catids; 		// 当前顶级栏目以及子栏目ids
	public $top_id; 			// 置顶id	
	public $cache_time; 		// 缓存时间
	public $cache_path; 		// 缓存存放路径
	public $cache_pre; 			// 缓存前缀
	public $order; 				// 排序
	public $limit; 				// 分页限制
	public $page_num; 			// 每页显示数
	public $page; 				// 当前页
	public function __construct(){
		global $p_vars;
		// 显示总栏目id
		$this->catid = $p_vars['v_catid'];
		// 置顶文章id
		$this->top_id = $p_vars['v_top_id'] ? $p_vars['v_top_id'] : 0 ;
		// 当前显示栏目
		$this->now_catid = isset($_GET['catid']) ? $_GET['catid'] : $this->catid;
		// 当前顶级栏目以及子栏目ids
		$this->now_catids = $this->_get_now_catids($this->now_catid);
		$this->now_catids = trim($this->now_catids,',');
		// 缓存时间
		$this->cache_time = (int)$p_vars['v_cache_time'];
		// 缓存目录
		$this->cache_path = DISCUZ_ROOT.'./data/sysdata/';
		// 缓存前缀
		$this->cache_pre = "ii_video/cache_ii_video_";
		
		// 每页显示数
		$this->page_num = $p_vars['v_page_num'] ? $p_vars['v_page_num'] : 15;
		// 当前页
		$this->page = isset($_GET['page']) ? abs((int)$_GET['page']) : 1;
		
		//设置排序
		$this->_set_order();
		// 设置分页限制
		$this->_set_limit();
	}

	/**
	 * [show_page 获得分页显示]
	 * @return [type] [description]
	 */
	public function get_page(){
		$url = $_SERVER['REQUEST_URI'];
		$uarr = parse_url($url);
		parse_str($uarr['query'],$ustr);
		if(isset($ustr['page']))  unset($ustr['page']);
		$url = $uarr['path'] . '?' . http_build_query($ustr);

		$getcount = $this->get_count();
		$page_num = $this->page_num;
		$page = $this->page;
		return multi($getcount, $page_num, $page, $url, $maxpages = 0, $page = 10, $autogoto = FALSE, $simple = FALSE);


	}

	/**
	 * [get_count 获得条件搜索总数]
	 * @return [type] [description]
	 */
	public function get_count(){
		// 缓存
		$data = $this->_cache(__FUNCTION__);
		// 缓存无效
		if(!$data){
			$sql = "SELECT count(*) FROM " . DB::table('portal_article_title') . " AS a INNER JOIN " . DB::table('portal_article_count') . " AS c ON a.aid=c.aid WHERE  a.status=0 and a.catid in (" . $this->now_catids . ")";
			$re = DB::fetch_first($sql);
			$data = $re['count(*)'];
			// 写入缓存
			$this->_cache(__FUNCTION__ , $data);
		}
		return $data;
	}

	/**
	 * [get_now_cats_list 获得指定显示栏目的文章信息 关联portal_article_count表]
	 * @param  [type] $catid [栏目id]
	 * @return [array]        []
	 */
	public function get_now_cats_list($catid = null){
		// 缓存
		$data = $this->_cache(__FUNCTION__);
		// 缓存无效
		if(!$data){
			$sql = "SELECT * FROM " . DB::table('portal_article_title') . " AS a INNER JOIN " . DB::table('portal_article_count') . " AS c ON a.aid=c.aid INNER JOIN " . DB::table("portal_category") . " AS b ON b.catid=a.catid WHERE a.status=0 and a.catid in (" . $this->now_catids . ") " . $this->order . $this->limit;
			// var_dump($sql);
			$data = DB::fetch_all($sql);
			// var_dump($data);
			// 写入缓存
			$this->_cache(__FUNCTION__, $data);
		}
		return $data;
	}	
	/**
	 * [set_order 设置排序]
	 */
	private function _set_order(){
		$array = array(1,2);
		// 排序方式 1 文章发布时间倒序 2 查看数倒序 
		$type = isset($_GET['order']) ? $_GET['order'] : 1;
		$type = !in_array($type, $array) ? 1 : $type;
		switch ($type) {
			case 1:
				$this->order = " order by viewnum desc ";
				break;
			case 2:
				$this->order = " order by a.dateline desc ";
				break;
			
			default:
				$this->order = " order by a.dateline desc ";
				break;
		}

	}
	/**
	 * [set_limit 设置分页限制]
	 */
	private function _set_limit(){
		$page = $this->page;
		$this->limit = " limit " . ($page - 1) * $this->page_num . ',' . $this->page_num;
	}
	/**
	 * [get_now_category 获得当前显示栏目]
	 * @param  [type] $catid [description]
	 * @return [type]        [description]
	 */
	public function get_now_category($catid = null){
		$cid = is_null($catid) ? $this->now_catid : $catid;
		// 缓存
		$data = $this->_cache(__FUNCTION__);
		// 缓存无效
		if(!$data){
			$sql = "SELECT * FROM " . DB::table('portal_category') . " WHERE  closed=0 and catid=" . $cid;
			$data = DB::fetch_first($sql);
			// 写入缓存
			$this->_cache(__FUNCTION__  , $data);
		}
		
		return $data;
	}	
	/**
	 * [_get_sub_ids 当前顶级栏目以及子栏目ids]
	 * @param  [type] $top_id [description]
	 * @return [type]         [description]
	 */
	private function _get_now_catids($top_id){
		$sql = "SELECT catid FROM " . DB::table('portal_category') . " WHERE upid=" . $top_id;
		$re = DB::fetch_all($sql);
		$ids = '';
		if($re){
			foreach ($re as $v) {
				$ids .= $this->_get_now_catids($v['catid']);
			}
			
		}else{
			$ids .= ',' . $top_id;
		}

		$data = $re ? $top_id . "," . trim($ids,',') : $ids;
		return $data;

	}
	/**
	 * [get_top_article 获得置顶视频]
	 * @return [type] [description]
	 */
	public function get_top_article(){
		
		// 缓存
		$data = $this->_cache(__FUNCTION__);
		// 缓存无效
		if(!$data){
			$top_id = $this->top_id;
			$sql = "SELECT * FROM " . DB::table("portal_article_title") . " AS a JOIN " . DB::table("portal_category") . " AS b ON a.catid=b.catid WHERE a.aid=".$top_id;
			$re = DB::fetch_first($sql);
			if(!$re){
				$sql = "SELECT * FROM " . DB::table("portal_article_title") . " AS a JOIN " . DB::table("portal_category") . " AS b ON a.catid=b.catid WHERE a.catid in(".$this->now_catids.") ORDER BY a.dateline DESC LIMIT 1";
				
				$re = DB::fetch_first($sql);
			}
			$data = $re;
			// 写入缓存
			$this->_cache(__FUNCTION__ , $data);
		}
		return $data;
	}
	/**
	 * [get_sub_cat_list 获得子栏目及子栏目前3条记录]
	 * @param [number] [获得记录数量]
	 * @return [array] [description]
	 */
	public function get_sub_cat_list($catid=null,$limit = 3){
		// 视频频道id
		$catid = $catid ? (int)$catid : $this->catid;
		// 缓存
		$data = $this->_cache(__FUNCTION__);

		// 缓存无效
		if(!$data){
			$data = array();
			// 子栏目ids
			$sub_catids = $this->get_sub_category($catid);

			
			// 查询子栏目内容
			foreach ($sub_catids as $k=>$v) {
				$data[$k]['catid'] = $v['catid'];
				$data[$k]['catname'] = $v['catname'];
				$data[$k]['catlist'] = $this->get_cat_list_by_limit($v['catid'],$limit);

			}
			
			// 写入缓存
			$this->_cache(__FUNCTION__ , $data);
		}
		return $data;
	}

	public function get_cat_list_by_limit($catid,$limit = 3){
		if(!$catid) return array();
		$limit = " limit " . $limit;
		$now_catids = $this->_get_now_catids($catid);
		$now_catids = trim($now_catids,',');

		// 缓存
		$data = $this->_cache(__FUNCTION__);
		// 缓存无效
		if(!$data){
			$sql = "SELECT * FROM " . DB::table('portal_article_title') . " AS a INNER JOIN " . DB::table('portal_article_count') . " AS c ON a.aid=c.aid INNER JOIN " . DB::table("portal_category") . " AS b ON b.catid=a.catid WHERE a.status=0 and a.catid in (" . $now_catids . ") " . $this->order . $limit;
			// var_dump($sql);
			$data = DB::fetch_all($sql);
			// var_dump($data);
			// 写入缓存
			$this->_cache(__FUNCTION__, $data);
		}
		return $data;
	}


	/**
	 * [get_cat_list 获得所有栏目信息]
	 * @return [type] [description]
	 */
	public function get_sub_category($catid = null){
		$cid = is_null($catid) ? $this->catid : $catid;
		// 缓存
		$data = $this->_cache(__FUNCTION__.$cid);
		// 缓存无效
		if(!$data){
			$sql = "SELECT * FROM " . DB::table('portal_category') . " WHERE upid=" . $cid . " and closed=0 order by displayorder";
			$data = DB::fetch_all($sql);
			// 写入缓存
			$this->_cache(__FUNCTION__.$cid, $data);
		
		}
		return $data;
	}



	/**
	 * [get_category_by_catid 根据catid获得栏目信息]
	 * @param  [type] $upid [description]
	 * @return [type]       [description]
	 */
	public function get_category_by_catid($catid = null){
		$cid = is_null($catid) ? $this->catid : $catid;
		// 缓存
		$data = $this->_cache(__FUNCTION__ . $cid);
		// 缓存无效
		if(!$data){
			$sql = "SELECT * FROM " . DB::table('portal_category') . " WHERE  closed=0 and catid=" . $cid;
			$data = DB::fetch_first($sql);	
			// 写入缓存
			$this->_cache(__FUNCTION__ . $cid, $data);
		
		}
		
		return $data;
	}

	/**
	 * [_cache 缓存处理 返回或设置缓存数据]
	 * @param  [type] $name [缓存名称]
	 * @param  [type] $data [缓存数据]
	 * @return [type]       [description]
	 */
	private function _cache($name ,$data = null){
		if(!$name){
			// 删除所有视频数据缓存
			$tpl = dir(DISCUZ_ROOT.'./data/sysdata/ii_video/');
			while($entry = $tpl->read()) {
					@unlink(DISCUZ_ROOT.'./data/sysdata/ii_video/'.$entry);
			}
			$tpl->close();
			return false;
		}
		// 获得缓存文件
		$file = $this->cache_path . $this->cache_pre . $name ."_" .md5($_SERVER['REQUEST_URI']) . ".php";
		// 缓存时间
		$cache_time = $this->cache_time;
		/**1. 获取缓存*********************************/
		if(is_null($data) && filemtime($file) + $cache_time * 60 > time()){
			
			return is_file($file) ? include_once($file) : false;
		}
		/**2. 设置缓存*********************************/
		// 不进行缓存
		if(!$this->cache_time){
			return;
		}
		// 创建缓存文件夹
		$dir = dirname($file);
		if(!is_dir($dir)){
			dmkdir($dir, 0777);
		}
		// 写入缓存
		@require_once libfile('function/cache');
		writetocache($name ."_" . md5($_SERVER['REQUEST_URI']) , "return " . arrayeval($data) , $this->cache_pre);
		/***********************************/
	}

}